package com.hb.trans.mvp.presenter

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.OnLifecycleEvent
import android.location.Location
import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.hb.trans.bean.Base
import com.hb.trans.bean.City
import com.hb.trans.bean.Geocoder
import com.hb.trans.bean.School
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.SchoolsContract
import com.hb.trans.mvp.model.SchoolsModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class SchoolsPresenter @Inject constructor(model: SchoolsModel, var errorHandler: RxErrorHandler) :
    BasePresenter<SchoolsContract.View, SchoolsContract.Model>(model) {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun getCounties() {
        mModel.getCounties()
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<MutableMap<String, MutableList<City>>>>(errorHandler) {
                override fun next(r: Base<MutableMap<String, MutableList<City>>>) {
                    if (r.code == 200) {
                        mRootView?.setCounties(r.data)
                    } else {
                        mRootView?.showShortToast(r.message ?: "")
                    }
                }
            })
    }

    fun getSchools(district: String) {
        mModel.getSchools(district)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<MutableList<School>>>(errorHandler) {
                override fun next(r: Base<MutableList<School>>) {
                    if (r.code == 200) {
                        mRootView?.setupSchoolData(r.data)
                    } else {
                        mRootView?.showShortToast(r.message ?: "")
                    }
                }
            })
    }

    fun findSchools(name: String) {
        mModel.findSchools(name)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<MutableList<School>>>(errorHandler) {
                override fun next(r: Base<MutableList<School>>) {
                    if (r.code == 200) {
                        mRootView?.setupSchoolData(r.data)
                    } else {
                        mRootView?.showShortToast(r.message ?: "")
                    }
                }
            })
    }

    fun getLocationInfo(location: Location) {
        mModel.getLocationInfo(location)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Geocoder>(errorHandler) {
                override fun next(r: Geocoder) {
                    if (r.status == 0) {
                        val addressComponent = r.result.addressComponent
                        mRootView?.setCurrentLocationCity(
                            addressComponent.province,
                            addressComponent.city,
                            addressComponent.district
                        )
                    } else {
                        mRootView?.showShortToast("获取城市信息失败，请返回重试")
                    }
                }
            })
    }


}